import 'package:enterprise_v6_app/app/core/ext/time.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_curve_entity.dart';
import 'package:get/get.dart';

enum HazardMonitorCurveType {
  liquid,
  pressure,
  temperature,
  ;

  String get name {
    switch (this) {
      case HazardMonitorCurveType.liquid:
        return '液位';
      case HazardMonitorCurveType.pressure:
        return '压力';
      case HazardMonitorCurveType.temperature:
        return '温度';
    }
  }

  static HazardMonitorCurveType? fromStr(String str) {
    switch (str) {
      case '液位':
        return HazardMonitorCurveType.liquid;
      case '压力':
        return HazardMonitorCurveType.pressure;
      case '温度':
        return HazardMonitorCurveType.temperature;
    }
    return null;
  }
}

class HazardMonitorCurveController extends GetxController {
  final curveType = HazardMonitorCurveType.liquid.obs;

  var cache = <HazardCurveEntity>[];

  Future<List<HazardCurveEntity>> getCurveData(String equipCode) async {
    if (cache.isEmpty) {
      final time = DateTime.now();
      final res = await HttpUtil().generateGetRequest<List<HazardCurveEntity>>(
        Interface.getHazardCurveHistory,
        queryParameters: {
          'equipCode': equipCode,
          'startDate': time.subtract(const Duration(days: 1)).toStr,
          'endDate': time.toStr,
        },
      ).execute(isolate: true);
      cache = res.data ?? [];
      cache.sort((a, b) {
        if (a.lastCollectTime == null || b.lastCollectTime == null) {
          return 1;
        }
        return a.lastCollectTime!.compareTo(b.lastCollectTime!);
      });
    }
    final data = List<HazardCurveEntity>.from(cache);
    data.removeWhere((element) => element.targetTypeDicName != curveType.value.name);
    return data;
  }
}
